package com.lc.headline.controller;

import com.lc.headline.common.Result;
import com.lc.headline.common.ResultCodeEnum;
import com.lc.headline.pojo.NewsUser;
import com.lc.headline.service.NewsUserService;
import com.lc.headline.service.impl.NewsUserServiceImpl;
import com.lc.headline.util.JwtHelper;
import com.lc.headline.util.MD5Util;
import com.lc.headline.util.WebUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;


import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author liuchun
 */
@WebServlet("/user/*")
public class NewsUserController extends BaseController{

    private NewsUserService newsUserService = new NewsUserServiceImpl();


    //登录校验
    protected void checkLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String token = request.getHeader("token");
        Result result = Result.build(null,ResultCodeEnum.NOTLOGIN);
        if (token != null && !(JwtHelper.isExpiration(token))){
            result = Result.ok(null);
        }
        WebUtil.writeJson(response,result);
    }

    protected void regist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        NewsUser newsUser = WebUtil.readJson(request, NewsUser.class);
        Integer rows = newsUserService.regist(newsUser);
        Result result = Result.build(null,ResultCodeEnum.USERNAME_USED);
        if (rows > 0){//注册成功
            result = Result.ok(null);
        }
        WebUtil.writeJson(response,result);
    }

    //注册是校验用户名是否被占用
    protected void checkUserName(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       //接收参数
        String username = request.getParameter("username");
        Result result = Result.ok(null);
        NewsUser newsUser = newsUserService.findUserByName(username);
        if (newsUser != null){//用户名已经在数据库存在
            result = Result.build(null,ResultCodeEnum.USERNAME_USED);
        }
        WebUtil.writeJson(response,result);
    }

    //接收token,根据token查询完整的用户信息
    protected void getUserInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获得请求头中token
       String token = request.getHeader("token");
       Result result = Result.build(null,ResultCodeEnum.NOTLOGIN);
       //如果token不为空
       if (token != null){
           if (!(JwtHelper.isExpiration(token))){//token没有过期
               Integer uid = JwtHelper.getUserId(token).intValue();
               NewsUser newsUser = newsUserService.findUserByUid(uid);
               newsUser.setUserPwd("");
               Map data = new HashMap();
               data.put("loginUser",newsUser);
               result = Result.ok(data);
           }
       }
       WebUtil.writeJson(response,result);
    }

    //用户登陆
    protected void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取用户名参数,这里数据是JSON格式的，这里不熟
//        String username = request.getParameter("username");
//        System.out.println(username);
//        String userPwd = request.getParameter("userPwd");
        NewsUser newsUser = WebUtil.readJson(request,NewsUser.class);
        String username  = newsUser.getUsername();
        String userPwd = newsUser.getUserPwd();
        //根据用户名去数据库中查询相关信息
        NewsUser  loginUser = newsUserService.findUserByName(username);
        Result result = null;
        if (loginUser != null){//有对应的用户名
            if (MD5Util.encrypt(userPwd).equals(loginUser.getUserPwd())){//密码正确
                Map data = new HashMap();
                //得到一个用户 id的token对象
                String  token  = JwtHelper.createToken(loginUser.getUid().longValue());
                data.put("token",token);
                result = Result.ok(data);
            }else {//密码错误
               result = Result.build(null, ResultCodeEnum.PASSWORD_ERROR);
            }
        }else {//用户名有误
                result = Result.build(null,ResultCodeEnum.USERNAME_ERROR);
        }
        //响应给客户端
        WebUtil.writeJson(response,result);
    }
}
